<?php 
namespace Home\Controller;
use \Think\Controller;
header("content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
class UserController extends Controller
{
	// 加载登录模版
	public function login()
	{
		$this->display('User/login');
	}

	// 加载注册模版
	public function regist()
	{
		$this->display('User/regist');
	}

	// 生成验证码
	public function verify()
	{
		$Verify = new \Think\Verify();
		$Verify->fontSize = 16;
		$Verify->length = 4;
		$Verify->useNoise = false;
		return $Verify->entry();
	}

	// 执行注册
	public function adduser()
	{
		$_POST['regtime'] = time();
		$_POST['password'] = md5($_POST['password']);
		$_POST['userimage'] = "1470310554.jpg";
		$user = D('user');

		// ajax请求回应,判断用户名唯一
		$name = $_REQUEST['username'];
		$where['username'] = $name;
		$data = $user->where($where)->find();
		if($data){
			$this->ajaxReturn('1');
		}else{
			if (!$user->create()) {

				$this->error($user->getError());
			}else{
				if($user->add() > 0){
					$this->success('注册成功',U('User/login'));
				}else{

					$this->error('注册失败',U('User/regist'));
				}
			}
		}
	}

	// 执行登录
	public function doLogin()
	{
		$user = M('user');
		$where['password'] = md5($_POST['password']);
		$where['username'] = $_POST['username'];
		$data = $user->where($where)->find();
		if (!empty($data)) {
			if ($data['status'] == 0) {
				$this->error('您的账号状态异常!请反思最近做过什么......','',5);
			} else {
				$_SESSION['username'] = $data['username'];
				$_SESSION['uid'] = $data['uid'];
				$this->redirect('Index/index');
			}
		} else {
			$this->error('登录失败 ! 请确认信息后重试...',U('User/login'));
		}
		
	}

	// 退出登录
	public function logout()
	{
		unset($_SESSION['username']);
		unset($_SESSION['uid']);
		$this->redirect('Index/index');
	}


	// 打卡得1经验
	public function score()
	{
		if (empty($_SESSION['uid'])) {
			$this->ajaxReturn('0');
		} else {
			$id = $_SESSION['uid'];
			$user = M('user');
			$where['uid'] = $id;
			$userInfo = $user->where($where)->find();
			// 签到加一分
			$data['score'] = $userInfo['score']+1;
			// 存入今天的时间戳
			$data['signtime'] = time();
			if ($user->where("uid={$id}")->save($data)) {
				echo  '1';
			}else{
				echo  '0';
			}
		}
	}

	// 忘记密码
	public function forget()
	{
		$this->display('User/forget');
	}

	// 密码重置
	public function test()
	{
		$secret = M('secret');
		$where['username'] = $_POST['username'];
		$secretdata = $secret->where($where)->select();
		$data = array();
		foreach ($secretdata as $k => $v ){
			$data[] = $v['content'].":".$v['answer'];
		}
		$post = array();
		$post[] = $_POST['secreta'].":".$_POST['answer00'];
		$post[] = $_POST['secretb'].":".$_POST['answer01'];
		$post[] = $_POST['secretc'].":".$_POST['answer02'];
		if(in_array($post[0], $data)&&in_array($post[1], $data)&&in_array($post[2], $data)){
			$user = M('user');
			$map['password'] = md5($_POST['password']);
			if($user->where($where)->save($map)){
				$this->success('恭喜你 ! 重置成功!');
			}else{
				$this->error('重置失败...');
			}
		}else{
			$this->error('密保问题验证失败...');
		}
	}

	// 用户密保设置
	public function secret()
	{
		$this->display('User/secret');
	}

	// 保存密保问题
	public function save_secret()
	{
		$secret = M('secret');
		if (count($_POST) >= 3) {
			$data = array();
			$data[0]['content'] = $_POST['secreta'];
			$data[0]['answer'] = $_POST['answer00'];
			$data[0]['username'] = $_SESSION['username'];
			$data[1]['content'] = $_POST['secretb'];
			$data[1]['answer'] = $_POST['answer01'];
			$data[1]['username'] = $_SESSION['username'];;
			$data[2]['content'] = $_POST['secretc'];
			$data[2]['answer'] = $_POST['answer02'];
			$data[2]['username'] = $_SESSION['username'];
			foreach ($data as $key => $value) {
				$result = $secret->add($value);
				if (!$result) {
					$this->error('设置密保失败...');
				}
			}
			$this->success('设置密保成功!');
		} else {
			$this->error('请不要留空...');
		}
		
		
	}

}